/** 
 
*/
import React, { PureComponent } from 'react';
import { PrimaryC, DATA_ENTRY, AdvancedSearch, ContentPackage } from '../../../comps';
import { message, Modal } from 'antd';
import { connect } from 'dva';
import moment from 'moment';
import 'moment/locale/zh-cn';
import DataImportTable from './DataImportTable';
import UploadImport from './UploadImport';
const { confirm } = Modal;

class DataImportNew extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      selectedRowKeys: [], //选中数据
      date: new Date(),
      now: new Date(),
      submitLoading: false, //提交按钮加载状态
      deleteLoading: false, //删除按钮加载状态
      withdrawLoading: false, // 回溯按钮加载状态
      ref: null,
      visible: false, //导入格式modal
    };
  }

  componentDidMount() {}

  //选中数据发生改变
  rowSelectionChange = (keys, rows) => {
    this.setState({ selectedRowKeys: rows });
  };

  //上传成功后
  uploadImportSuccess = () => {
    const { ref, date } = this.state;
    ref.getData(moment(date).format('YYYYMMDD'));
  };
  //提交数据--处理
  submitData = (e) => {
    const { dispatch } = this.props;
    const { selectedRowKeys, ref, date, submitLoading } = this.state;
    if (submitLoading) return;
    if (selectedRowKeys.length > 0) {
      this.setState({
        submitLoading: true,
      });

      dispatch({
        type: 'dataImportM/submitData',
        payload: selectedRowKeys.map((x) => x.id),
        callback: (type) => {
          if (type == 'success') {
            ref.getData(moment(date).format('YYYYMMDD'), () => {
              this.setState({
                submitLoading: false,
              });
            });
          }
        },
      });
    }
  };

  //撤回数据--撤回
  withdrawData = (e) => {
    const { dispatch } = this.props;
    const { selectedRowKeys, ref, date, withdrawLoading } = this.state;
    if (withdrawLoading) return;
    if (selectedRowKeys.length > 0) {
      this.setState({
        withdrawLoading: true,
      });
      dispatch({
        type: 'dataImportM/withdrawData',
        payload: selectedRowKeys.map((x) => x.id),
        callback: (type) => {
          if (type == 'success') {
            ref.getData(moment(date).format('YYYYMMDD'), () => {
              this.setState({
                withdrawLoading: false,
              });
            });
          }
        },
      });
    }
  };

  //  点击删除
  onDeleteItem2 = () => {
    const { dispatch } = this.props;
    const { selectedRowKeys, date, ref } = this.state;
    if (selectedRowKeys.length > 0) {
      confirm({
        title: '确认是否删除?',
        okText: '确认',

        cancelText: '取消',
        onOk: () => {
          this.setState({
            deleteLoading: true,
          });
          dispatch({
            type: 'dataImportM/deleteTAdate',
            payload: selectedRowKeys.map((x) => x.id),
            callback: (type) => {
              if (type == 'success') {
                ref.getData(moment(date).format('YYYYMMDD'), () => {
                  this.setState({
                    deleteLoading: false,
                  });
                });
              }
            },
          });
        },
      });
    } else {
      message.success(`请勾选要删除的数据！`);
    }
  };

  //按钮高亮逻辑  处理(submitData)、回溯(withdrawData)、删除(deleteTAdate)
  checkDisable = (type) => {
    const { selectedRowKeys, now } = this.state;
    let disabled = true;
    const time = moment(now.getTime()).format('YYYY-MM-DD');

    if (selectedRowKeys.length > 0) {
      disabled = false;
      selectedRowKeys.map((row) => {
        if (row.importDate && time == moment(row.importDate).format('YYYY-MM-DD')) {
          // 判断是否在当天
          if (!(row.isHandle != '1' && row.isHandle2 != '1')) {
            //判断交收日状态和确认日状态是否处理
            if (type === 'submitData' || type === 'deleteTAdate') {
              disabled = true;
            }
          }
          if (!(row.isHandle == '1' || row.isHandle2 == '1')) {
            //判断交收日状态和确认日状态是否处理
            if (type === 'withdrawData') {
              disabled = true;
            }
          }
        } else {
          disabled = true;
        }
      });
    }
    return disabled;
  };

  render() {
    const { dispatch } = this.props;
    const that = this;

    const searchProps = {
      dataSource: [
        {
          dataIndex: 'importDate',
          title: '日期',
          type: DATA_ENTRY.SINGLEDATE,
          initialValue: moment(),
          onChange: (e) => this.setState({ date: e }),
        },
      ],
      allowQuery: false,
      allowClear: false,
    };

    return (
      <ContentPackage
        title='TA数据导入'
        contentHtml={
          <div style={{ height: '100%' }}>
            <div style={{ marginBottom: '20px' }}>
              <AdvancedSearch {...searchProps} />
            </div>
            <div>
              <PrimaryC loading={this.state.submitLoading} disabled={this.checkDisable('submitData')} onClick={() => this.submitData()} title={'处理'} style={{ margin: '5px' }} />
              {/* <PrimaryC
                        loading={this.state.withdrawLoading}
                        disabled={this.checkDisable('withdrawData')}
                        onClick={() => this.withdrawData()}
                        title={'回溯'}
                        style={{ margin: '5px' }}
                    /> */}
              <UploadImport
                dispatch={dispatch}
                that={that}
                primaryProps={{
                  helper: (
                    <div>
                      <div>请尽量在每日9:30之前完成产品申购/赎回数据的导入，并点击“处理“。</div>
                      <div>9:30之后建议通过”库存管理/资金管理“进行资金调整。</div>
                    </div>
                  ),
                }}
              />
              <PrimaryC
                onClick={() => {
                  this.setState({ visible: true });
                }}
                title={'导入格式'}
                style={{ margin: '5px' }}
              />
              <PrimaryC
                disabled={this.checkDisable('deleteTAdate')}
                loading={this.state.deleteLoading}
                onClick={() => this.onDeleteItem2()}
                title={'删除'}
                style={{ margin: '5px' }}
              />
            </div>
            <div>
              <DataImportTable
                onRef={(e) => this.setState({ ref: e })}
                date={moment(this.state.date).format('YYYYMMDD')}
                rowSelectionChange={this.rowSelectionChange}
              ></DataImportTable>
            </div>
            <Modal
              title='导入格式'
              visible={this.state.visible}
              onCancel={() => {
                this.setState({ visible: false });
              }}
              destroyOnClose={true}
              footer={false}
              width={1020}
            >
              <span style={{ fontSize: '17px' }}>文件类型:txt,excel</span>
              <br />
              <span style={{ fontSize: '17px' }}>
                导入说明:业务类型,基金产品代码,交易申请日期,成交份额,成交金额,总费用,交易费,托管费, 印花税,后收费,其他费用,归基金资产费用,交收日期,处理日期,单位分红,销售机构
              </span>
              <br />
              <span style={{ fontSize: '17px' }}>导入格式:02,940032,20180912,50000,50000,0,0,0,0,0,0,0,20190517,20190517,0,TA</span>
              <br />
              <span style={{ fontSize: '17px' }}>
                业务类型:申购(02)、赎回(03)、基金成立(50)、基金转换出(13)、基金转换入(16)、现金分红（74）、再投资1（73）、再投资2（75）、强增(70)、强减(71)
              </span>
              <br />
              <span style={{ fontSize: '17px' }}>交易申请日期:为上一个工作日 YYYYMMDD</span>
              <br />
              <span style={{ fontSize: '17px' }}>
                成交份额: 对再投资1（73）为0 <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;对再投资2（75）为红利再投资份额.
                <br />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;其中：申购、基金成立、基金转换入、再投资、强增表示基金份额增加，其他业务表示减少。
              </span>
              <br />
              <span style={{ fontSize: '17px' }}>
                成交金额:对申购、基金成立、基金转换入、再投资、强增，表示基金资产增加（不包含手续费），对赎回、基金转换出、分现金、强减表示基金资产减少（含手续费）
                <br />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;其中：（74）业务只包含现金分红的金额，（73）包含再投资的金额
              </span>
              <br />
              <span style={{ fontSize: '17px' }}>总费用:包括所有税费</span>
              <br />
              <span style={{ fontSize: '17px' }}>印花税:对分红为所得税</span>
              <br />
              <span style={{ fontSize: '17px' }}>后收费:赎回时的后收认、申购费，基金转换时的补差费</span>
              <br />
              <span style={{ fontSize: '17px' }}>归基金资产费用:赎回、基金转换的交易费中，归基金资产的部分</span>
              <br />
              <span style={{ fontSize: '17px' }}>交收日期:资金划账日期(基金成立为成立确认日，分红：红利发放日、再投资日；强增、强减为确认日期) 73的交收日期同74的一样</span>
              <br />
              <span style={{ fontSize: '17px' }}>处理日期:当前日期</span>
              <br />
              <span style={{ fontSize: '17px' }}>单位分红:单位基金分红金额</span>
              <br />
              <span style={{ fontSize: '17px' }}>销售机构:销售商代码</span>
            </Modal>
          </div>
        }
      />
    );
  }
}
export default connect(({ dataImportM, loading }) => ({
  dataImportM,
  loading,
}))(DataImportNew);
